{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding = utf-8 -*-\n",
    "import requests\n",
    "headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}\n",
    "url='https://www.zhihu.com/api/v3/feed/topstory/recommend?session_token=3e6407312ed4eb453b09c4342f5ebb59&desktop=true&page_number=4&limit=6&action=down&after_id=17'\n",
    "cookies = dict(cookies_are='_zap=d5d4f958-165b-4b92-ac25-b4368dd0f636; d_c0=\"AHAhNJROtg6PTgsLf9VX1XotGnMFk0JBVM0=|1545532580\"; q_c1=300abcd6bdff4be381c0cd88834b5018|1545532583000|1545532583000; _xsrf=279f4dc4-a6b2-4828-929e-2d99e9a48b60; tgw_l7_route=a37704a413efa26cf3f23813004f1a3b; capsion_ticket=\"2|1:0|10:1568035282|14:capsion_ticket|44:ZWQ2OGQzMmY1ZTc3NGZjMGJjMjIzNjBiODM0M2I2MjI=|b72c026599c7df3be15c44d1094485576eb28ace35a887dcca304875387f1d03\"; z_c0=\"2|1:0|10:1568035289|4:z_c0|92:Mi4xaGJNNkFBQUFBQUFBY0NFMGxFNjJEaVlBQUFCZ0FsVk4yWjlqWGdDLTBueDlHZ3ZtS1JaU1NTSVVwRUs1bTVxRDhR|5ae477fe41722e34972cec9d5be9cf3f95b969e48fdf6b1dae2ed7f0877a28b6\"; tst=r')\n",
    "r = requests.get(url, cookies=cookies,headers = headers)\n",
    "message=r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'question'\n"
     ]
    }
   ],
   "source": [
    "zhihu_list=[]\n",
    "for i in message['data']:\n",
    "    q_list=[]\n",
    "    try:\n",
    "        q_list.extend([i['id'],i['target']['question']['title'],i['target']['question']['answer_count'],i['target']['question']['follower_count'],i['target']['author']['name'],i['action_text']])\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "    finally:\n",
    "        zhihu_list.append(q_list) \n",
    "import csv\n",
    "with open('zhihu.csv','w',encoding='utf_8_sig') as f:\n",
    "    cw= csv.writer(f)\n",
    "    for item in zhihu_list:\n",
    "        cw.writerow(item) #将列表的每个元素写到csv文件的一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
